﻿using System.Linq;
using System.Modules.ExceptionHanlding;
using Microsoft.SharePoint;

namespace System.Modules.SharePoint.Extensions
{
    /// <summary>
    /// SP Content Type Collection Extensions
    /// </summary>
    public static class SPContentTypeCollectionExtensions
    {
        #region Ensure
        /// <summary>
        /// This Method ensures content type
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="spContentType"></param>
        public static void Ensure(this SPContentTypeCollection collection, SPContentType spContentType)
        {
            if (collection != null)
            {
                try
                {
                    if (!collection.Cast<SPContentType>().Any(contentType => contentType.Parent.Id.Equals(spContentType.Id)))
                    {
                        collection.Add(spContentType);
                    }
                }
                catch (Exception exception)
                {
                    exception.Handle();
                }
            }
        }
        #endregion
    }
}